<?php
namespace App\Http\Controllers\AdminApi;
use App\Model\BlockTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Sys\JsonRespose;
class BlockController extends ApiController{
    public function getBlock(){
        $group_id = \Request::get('group_id');  //数据组id
        return JsonRespose::done(BlockTable::getBlock($group_id));
    }
    //修改保存block数据
    public function postSave(){
        $form_data = \Request::get('form_data');
        if (!$form_data) {
            return JsonRespose::fail('form_data 数据不存在');
        } else {
            $form_data = json_decode($form_data, true);
        }
        $ok = 0;
        foreach ($form_data as $k => $v) {
            $res = $db = BlockTable::ORM()->where('id', $v['id'])->update($v);
            if ($res) {
                $ok++;
            }
        }
        if ($ok) {
            return JsonRespose::success(array('edit_count' => $ok));
        } else {
            return JsonRespose::fail();
        }
    }
    //添加block数据
    public function postAdd(){
        $group_id = \Request::get('group_id');
        if (!$group_id) {
            return JsonRespose::fail('group_id不存在');
        }

        $res = $db = BlockTable::ORM()->insertGetId(['group_id' => $group_id]);

        if ($res) {
            return JsonRespose::success(array('id' => $res));
        } else {
            return JsonRespose::fail();
        }
    }
    //删除block数据
    public function postDel(){
        $id = \Request::get('id');
        if (!$id) {
            return JsonRespose::fail('id不存在');
        }
        $res = $db = BlockTable::ORM()->where(['id' => $id])->delete();
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
}
